package com.hds.modules.sys.utils;

import com.hds.common.exception.RRException;
import com.hds.modules.app.entity.ActivationCodeEntity;
import com.hds.modules.app.utils.DateUtils;
import com.hds.modules.sys.entity.base.SysCompanyEntity;
import com.hds.modules.sys.entity.config.UploadFileConfig;
import com.hds.modules.sys.service.base.SysCompanyService;
import org.apache.commons.lang.StringUtils;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author guonan
 * @Email guonan@yewcn.com
 * @Description
 * @Date 2018/8/1 18:47
 */
public class ExcelUtil {

    private static Logger logger = LoggerFactory.getLogger(ExcelUtil.class);




    /**
     * 导出Excel
     */
    public static boolean exportExcel(List<ActivationCodeEntity> codes, String excelName,
                               UploadFileConfig uploadFileConfig, SysCompanyService sysCompanyService){
        excelName = StringUtils.isBlank(excelName) ? DateUtils.format(new Date(), "yyyyMMddHHmmss") : excelName;
        String filePath=uploadFileConfig.getActivateExcelPath() + "/" + excelName +".xls"; //文件路径
        logger.info("激活码导出路径:{}", filePath);
        // Create a workbook object
        Workbook workbook = new HSSFWorkbook();
        // Create sheet
        Sheet sheet = workbook.createSheet("红豆杉激活码列表");


        /**
         * 标题
         */
        Row row = sheet.createRow((short) 0);
        sheet.setDefaultColumnWidth(20); //单元格宽度


        // 单元格样式
        CellStyle style = workbook.createCellStyle();
        Font font = workbook.createFont();
        font.setColor(HSSFColor.WHITE.index);
        font.setFontHeightInPoints((short)10); //字体大小
//        style.setBorderLeft(BorderStyle.DASH_DOT);
        style.setLeftBorderColor(HSSFColor.WHITE.index);
        style.setFont(font);

//        style.setAlignment(HorizontalAlignment.CENTER);
        style.setFillForegroundColor(IndexedColors.ROYAL_BLUE.getIndex());
        style.setFillPattern(CellStyle.SOLID_FOREGROUND);




        List<String> list = new ArrayList<>();
        list.add("激活码");
        list.add("卡号");
        list.add("激活码失效期");

        //title处理
        for (int i=0;i<list.size();i++){
            Cell cell = row.createCell(i);
            cell.setCellValue(list.get(i));
            cell.setCellStyle(style);
        }




        //内容处理
        int i=0;
        for (; i<codes.size(); i++) {
            Row row1 = sheet.createRow((short) i+1);
            ActivationCodeEntity code = codes.get(i);
            Integer companyId = code.getCompanyId();
            SysCompanyEntity company = sysCompanyService.selectById(companyId);

            String prefix = company!=null ? company.getCardPrefix() + ActivationCodeEntity.LEVELPREFIX + code.getLevelId() : null;



            style = workbook.createCellStyle();
//            style.setAlignment(HorizontalAlignment.CENTER);
            Cell cell1 = row1.createCell((short) 0);
            cell1.setCellValue(code.getActivationcode());
            cell1.setCellStyle(style);


            Cell cell2 = row1.createCell((short) 1);
            cell2.setCellValue(prefix + code.getCardNo());
            cell2.setCellStyle(style);


            Cell cell3 = row1.createCell((short) 2);
            cell3.setCellValue(DateUtils.format(code.getGuarantee(), "yyyy-MM-dd HH:mm:ss"));
            cell3.setCellStyle(style);



        }

        try{
            FileOutputStream out = new FileOutputStream(filePath);
            workbook.write(out);//保存Excel文件
            out.close();//关闭文件流
            System.out.println("excel导出OK!");
            return true;
        }catch(Exception e){
            throw new RRException("excel导出异常");
        }

    }

}
